package cn.kgc.springcloud.controller;

import com.netflix.loadbalancer.IRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/2/16
 * @Description:
 */
@RestController
@RequestMapping("exam")
public class ExamController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("start")
    public String startExam(){

        System.out.println("--------考试完毕-------");

        //添加成绩  scoreService
        RestTemplate restTemplate = new RestTemplate();
        // 参数1  请求的接口地址   参数2  接口返回的数据类型
        String info = restTemplate.getForObject("http://"+getRandomHost()+"/score/add", String.class);

        return "startExam ok  --> "+info;
    }



    @RequestMapping("start2")
    public String startExam2(){

        System.out.println("--------考试完毕-------");

        // 参数 注册中心服务名称
        List<ServiceInstance> scoreservice = discoveryClient.getInstances("SCORESERVICE");
        // 获取一个通信节点
        ServiceInstance serviceInstance = scoreservice.get(new Random().nextInt(scoreservice.size()));
        URI uri = serviceInstance.getUri();
        System.out.println("uri = " + uri);  // http://localhost:8503   +  /score/add

        //添加成绩  scoreService
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri + "/score/add", String.class);

        return "startExam ok  --> "+info;
    }


    @RequestMapping("start3")
    public String startExam3(){

        System.out.println("--------考试完毕-------");

        ServiceInstance scoreservice = loadBalancerClient.choose("SCORESERVICE");

        URI uri = scoreservice.getUri();
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri + "/score/add", String.class);   // http://SCORESERVICE/score/add

        return "startExam ok  --> "+info;
    }

    @RequestMapping("start4")
    public String startExam4(){

        System.out.println("--------考试完毕-------");
        //  负载均衡策略 轮循
        String info = restTemplate.getForObject("http://SCORESERVICE/score/add", String.class);
        return "startExam ok  --> " +info;
    }

/*
* 1.访问策略单一
* 2.服务的管控不灵活  固定数据
* 3.服务健康状态 无法管控
* */
    public  String getRandomHost(){  //  随机策略
        ArrayList<String> host = new ArrayList<>();

        host.add("localhost:8503");
        host.add("localhost:8504");
        host.add("localhost:8505");

        Random random = new Random();
        int index = random.nextInt(host.size());
        return host.get(index);
    }

}
